package cn.com.css.taxInspection.workrisk.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.com.css.taxInspection.common.constants.Constants;
import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.gt3.entity.JcAjxxEntity;
import cn.com.css.taxInspection.gt3.service.JcAjxxService;
import cn.com.css.taxInspection.system.entity.SysUser;
import cn.com.css.taxInspection.system.service.UserService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import cn.com.css.taxInspection.workrisk.entity.WorkriskControlDispatchEntity;
import cn.com.css.taxInspection.workrisk.entity.WorkriskControlEntity;
import cn.com.css.taxInspection.workrisk.entity.WorkriskDispatchReportEntity;
import cn.com.css.taxInspection.workrisk.service.WorkriskControlDispatchService;
import cn.com.css.taxInspection.workrisk.service.WorkriskControlService;
import cn.com.css.taxInspection.workrisk.service.WorkriskDispatchReportService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;

/**
 * ${comments}
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2023-11-21 15:56:05
 */
@Controller
@RequestMapping("/workflow/dispatch")
public class WorkriskControlDispatchController {
	@Autowired
	private WorkriskControlDispatchService workriskControlDispatchService;
	@Autowired
	private WorkriskControlService workriskControlService;
	@Autowired
	private JcAjxxService jcAjxxService;
	@Autowired
	private HttpSessionService httpSessionService;
	@Autowired
	private WorkriskDispatchReportService workriskDispatchReportService;
	
	@Autowired
	private UserService userService;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "未参加")
	@PostMapping("list")
	@ResponseBody
	public DataResult list(@RequestBody WorkriskControlDispatchEntity workriskControlDispatch) {

		String currentUserId = httpSessionService.getCurrentUserId();
		String currentRolesId = httpSessionService.getCurrentRolesId();
		
		Page page = new Page(workriskControlDispatch.getPage(), workriskControlDispatch.getLimit());
		LambdaQueryWrapper<WorkriskControlDispatchEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		queryWrapper.eq(WorkriskControlDispatchEntity::getDispatchMan, currentUserId);
		queryWrapper.isNull(WorkriskControlDispatchEntity::getAttend);
		
		IPage<WorkriskControlDispatchEntity> iPage = workriskControlDispatchService.page(page, queryWrapper);
		
		List<WorkriskControlDispatchEntity> records = iPage.getRecords();
		for (WorkriskControlDispatchEntity workriskControlDispatchEntity : records) {
			
			String controlid = workriskControlDispatchEntity.getControlid();
			String jcajuuid = workriskControlDispatchEntity.getJcajuuid();
			
			WorkriskControlEntity workriskControlEntity = workriskControlService.getById(controlid);
			
			JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(jcajuuid);
			
			String nsrmc = jcAjxxEntity.getNsrmc();
			String nsrsbh = jcAjxxEntity.getNsrsbh();
			String ajmc = jcAjxxEntity.getAjmc();
			String jcajbh = jcAjxxEntity.getJcajbh();
			
			
			String details = workriskControlEntity.getDetails();
			
			workriskControlDispatchEntity.setNsrmc(nsrmc);
			workriskControlDispatchEntity.setNsrsbh(nsrsbh);
			workriskControlDispatchEntity.setAjmc(ajmc);
			workriskControlDispatchEntity.setDetails(details);
			workriskControlDispatchEntity.setPostion(currentRolesId);
			workriskControlDispatchEntity.setJcajbh(jcajbh);
			
			// workriskControlDispatchEntity.s
			
			String reportid = workriskControlDispatchEntity.getReportid();
			if(TsBqUtils.isNotNull(reportid)){
				WorkriskDispatchReportEntity dispatchReport = workriskDispatchReportService.getById(reportid);
				
				
				workriskControlDispatchEntity.setAjjzqk(dispatchReport.getAjjzqk());
				workriskControlDispatchEntity.setCzkn(dispatchReport.getCzkn());
				workriskControlDispatchEntity.setXybwcgznr(dispatchReport.getXybwcgznr());
				workriskControlDispatchEntity.setKzyj(dispatchReport.getKzyj());
				workriskControlDispatchEntity.setJjwqyj(dispatchReport.getJjwqyj());
			}
			
			
			
		}
		
		
		return DataResult.success(iPage);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "已参加")
	@PostMapping("list1")
	@ResponseBody
	public DataResult list1(@RequestBody WorkriskControlDispatchEntity workriskControlDispatch) {

		String currentUserId = httpSessionService.getCurrentUserId();
		String currentRolesId = httpSessionService.getCurrentRolesId();
		
		Page page = new Page(workriskControlDispatch.getPage(), workriskControlDispatch.getLimit());
		LambdaQueryWrapper<WorkriskControlDispatchEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		queryWrapper.eq(WorkriskControlDispatchEntity::getDispatchMan, currentUserId);
		queryWrapper.eq(WorkriskControlDispatchEntity::getAttend, "Y");

		IPage<WorkriskControlDispatchEntity> iPage = workriskControlDispatchService.page(page, queryWrapper);
		
		List<WorkriskControlDispatchEntity> records = iPage.getRecords();
		for (WorkriskControlDispatchEntity workriskControlDispatchEntity : records) {
			
			String controlid = workriskControlDispatchEntity.getControlid();
			String jcajuuid = workriskControlDispatchEntity.getJcajuuid();
			
			WorkriskControlEntity workriskControlEntity = workriskControlService.getById(controlid);
			
			JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(jcajuuid);
			
			String nsrmc = jcAjxxEntity.getNsrmc();
			String nsrsbh = jcAjxxEntity.getNsrsbh();
			String ajmc = jcAjxxEntity.getAjmc();
			String jcajbh = jcAjxxEntity.getJcajbh();
			
			String details = workriskControlEntity.getDetails();
			
			workriskControlDispatchEntity.setNsrmc(nsrmc);
			workriskControlDispatchEntity.setNsrsbh(nsrsbh);
			workriskControlDispatchEntity.setAjmc(ajmc);
			workriskControlDispatchEntity.setDetails(details);
			
			workriskControlDispatchEntity.setPostion(currentRolesId);
			
			workriskControlDispatchEntity.setJcajbh(jcajbh);
			
			String reportid = workriskControlDispatchEntity.getReportid();
			if(TsBqUtils.isNotNull(reportid)){
				WorkriskDispatchReportEntity dispatchReport = workriskDispatchReportService.getById(reportid);
				
				
				workriskControlDispatchEntity.setAjjzqk(dispatchReport.getAjjzqk());
				workriskControlDispatchEntity.setCzkn(dispatchReport.getCzkn());
				workriskControlDispatchEntity.setXybwcgznr(dispatchReport.getXybwcgznr());
				workriskControlDispatchEntity.setKzyj(dispatchReport.getKzyj());
				workriskControlDispatchEntity.setJjwqyj(dispatchReport.getJjwqyj());
			}
			
			
		}
		
		
		return DataResult.success(iPage);
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "confirm")
	@PostMapping("confirm")
	@ResponseBody
	public DataResult confirm(@RequestBody WorkriskControlDispatchEntity workriskControlDispatch) {
		String currentUserId = httpSessionService.getCurrentUserId();
		Date date = new Date();
		String id = workriskControlDispatch.getId();
		WorkriskControlDispatchEntity controlDispatch = workriskControlDispatchService.getById(id);
		String controlid = controlDispatch.getControlid();
		String reportid = controlDispatch.getReportid();
		controlDispatch.setAttend("Y");
		workriskControlDispatchService.updateById(controlDispatch);

		// 判断是否全部完成

		LambdaQueryWrapper<WorkriskControlDispatchEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		queryWrapper.isNull(WorkriskControlDispatchEntity::getAttend);
		queryWrapper.eq(WorkriskControlDispatchEntity::getControlid, controlid);

		List<WorkriskControlDispatchEntity> list = workriskControlDispatchService.list(queryWrapper);
		
		if(TsBqUtils.isNull(list)){
			WorkriskControlEntity workriskControlEntity = workriskControlService.getById(controlid);
			workriskControlEntity.setStateCode("3");
			workriskControlEntity.setStateName("调度会完成");
			workriskControlService.updateById(workriskControlEntity);
			// 更新报告
			
			WorkriskDispatchReportEntity dispatchReportEntity = workriskDispatchReportService.getById(reportid);
			
			
			dispatchReportEntity.setStatecode("2"); //  1 待确认、2 已确认、 3 退回
			dispatchReportEntity.setUpdateTime(date);
			dispatchReportEntity.setUpdateId(currentUserId);
			
			workriskDispatchReportService.updateById(dispatchReportEntity);
			
			
		}

		return DataResult.success();
		
	}

	
	@ApiOperation(value = "report")
	@PostMapping("report")
	@ResponseBody
	public DataResult report(@RequestBody WorkriskDispatchReportEntity workriskDispatchReportEntity) {
		// 临时ID
		String id = workriskDispatchReportEntity.getControlId();
		String currentUserId = httpSessionService.getCurrentUserId();
		String currentDeptId = httpSessionService.getCurrentDeptId();
		
		Date date = new Date();
		if(TsBqUtils.isNull(id)){
			return DataResult.fail("controlId未获取到！");
		}
		String ajjzqk = workriskDispatchReportEntity.getAjjzqk();
		if(TsBqUtils.isNull(ajjzqk)){
			return DataResult.fail("案件进展情况未填写！");
		}

		String czkn = workriskDispatchReportEntity.getCzkn();
		if(TsBqUtils.isNull(czkn)){
			return DataResult.fail("存在困难未填写！");
		}

		String xybwcgznr = workriskDispatchReportEntity.getXybwcgznr();
		if(TsBqUtils.isNull(xybwcgznr)){
			return DataResult.fail("下一步需要完成工作内容未填写！");
		}

		String kzyj = workriskDispatchReportEntity.getKzyj();
		if(TsBqUtils.isNull(kzyj)){
			return DataResult.fail("科长意见未填写！");
		}

		String jjwqyj = workriskDispatchReportEntity.getJjwqyj();
		if(TsBqUtils.isNull(jjwqyj)){
			return DataResult.fail("纪检委员意见未填写！");
		}
		WorkriskControlDispatchEntity workriskControlDispatchEntity = workriskControlDispatchService.getById(id);
		
		workriskControlDispatchEntity.setAttend("Y");
		workriskControlDispatchService.updateById(workriskControlDispatchEntity);
		
		String controlid = workriskControlDispatchEntity.getControlid();
		// WorkriskControlEntity workriskControlEntity = workriskControlService.getById(controlid);
		WorkriskDispatchReportEntity dispatchReport = new WorkriskDispatchReportEntity();
		
		String uuid = TsBqUtils.getUUID().toString();
		dispatchReport.setId(uuid);

		dispatchReport.setJaxxuuidId(workriskControlDispatchEntity.getJcajuuid());
		dispatchReport.setControlId(controlid);
		// workriskDispatchReportEntity.setDispatchId();
		// workriskDispatchReportEntity.setCateId();
		dispatchReport.setStatecode("1"); //  1 待确认、2 已确认、 3 退回
		dispatchReport.setAjjzqk(ajjzqk);
		dispatchReport.setCzkn(czkn);
		dispatchReport.setXybwcgznr(xybwcgznr);
		dispatchReport.setKzyj(kzyj);
		dispatchReport.setJjwqyj(jjwqyj);
		dispatchReport.setTaxorgcode(currentDeptId);
		dispatchReport.setDeleted(Constants.DATA_NOT_DELETED);
		dispatchReport.setCreateTime(date);
		dispatchReport.setCreateId(currentUserId);
		
		workriskDispatchReportService.saveOrUpdate(dispatchReport);
		
		/*workriskControlEntity.setStateCode("3");
		workriskControlEntity.setStateName("出具案件调度报告");
		
		workriskControlService.updateById(workriskControlEntity);*/
		
		// 更新 WORKRISK_CONTROL_DISPATCH 表
		
		LambdaQueryWrapper<WorkriskControlDispatchEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		queryWrapper.eq(WorkriskControlDispatchEntity::getControlid, controlid);
		
		List<WorkriskControlDispatchEntity> list = workriskControlDispatchService.list(queryWrapper);
		
		for (WorkriskControlDispatchEntity controlDispatch : list) {
			controlDispatch.setStateCode("2");
			controlDispatch.setUpdateId(currentUserId);
			controlDispatch.setUpdateTime(date);
			controlDispatch.setReportid(uuid);
			
			workriskControlDispatchService.updateById(controlDispatch);
			
		}
		
		
		
		
		
		return DataResult.success();
		
	}
	
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "调度会报告")
	@PostMapping("listbg")
	@ResponseBody
	public DataResult listbg(@RequestBody WorkriskDispatchReportEntity workriskDispatchReportEntity) {

		String currentDeptId = httpSessionService.getCurrentDeptId();
		
		Page page = new Page(workriskDispatchReportEntity.getPage(), workriskDispatchReportEntity.getLimit());
		LambdaQueryWrapper<WorkriskDispatchReportEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		queryWrapper.like(WorkriskDispatchReportEntity::getTaxorgcode, currentDeptId.substring(0, 9));
		queryWrapper.eq(WorkriskDispatchReportEntity::getStatecode, "2");
		IPage<WorkriskDispatchReportEntity> iPage = workriskDispatchReportService.page(page, queryWrapper);
		
		List<WorkriskDispatchReportEntity> records = iPage.getRecords();
		
		for (WorkriskDispatchReportEntity dispatchReport : records) {
			
			String jaxxuuidId = dispatchReport.getJaxxuuidId();
			
			JcAjxxEntity jcAjxxEntity = jcAjxxService.getByJcajxxuuid(jaxxuuidId);
			
			dispatchReport.setNsrmc(jcAjxxEntity.getNsrmc());
			dispatchReport.setNsrsbh(jcAjxxEntity.getNsrsbh());
			dispatchReport.setAjmc(jcAjxxEntity.getAjmc());
			dispatchReport.setJcajbh(jcAjxxEntity.getJcajbh());
			dispatchReport.setLarq(String.valueOf(jcAjxxEntity.getLarq()));
			dispatchReport.setJcry(jcAjxxEntity.getJcry());
			
		}
		
		
		return DataResult.success(iPage);
	}
	
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "调度会情况")
	@PostMapping("controlid")
	@ResponseBody
	public DataResult controlid(@RequestBody WorkriskControlDispatchEntity workriskControlDispatchEntity) {

		
		String controlid = workriskControlDispatchEntity.getControlid();
		if(TsBqUtils.isNull(controlid)){
			
			return DataResult.fail("controlid未获取到");
		}
		
		Page page = new Page(workriskControlDispatchEntity.getPage(), workriskControlDispatchEntity.getLimit());
		LambdaQueryWrapper<WorkriskControlDispatchEntity> queryWrapper = Wrappers.lambdaQuery();
		// 查询条件示例
		queryWrapper.eq(WorkriskControlDispatchEntity::getControlid, controlid);
		
		IPage<WorkriskControlDispatchEntity> iPage = workriskControlDispatchService.page(page, queryWrapper);
		
		List<WorkriskControlDispatchEntity> records = iPage.getRecords();
		
		for (WorkriskControlDispatchEntity controlDispatch : records) {
			
			String dispatchMan = controlDispatch.getDispatchMan();
			
			SysUser sysUser = userService.getById(dispatchMan);
			if(TsBqUtils.isNull(sysUser)){
				
			}else{
				dispatchMan = sysUser.getNickName();
			}
			
			
			
			controlDispatch.setDispatchMan(dispatchMan);
			
		}
		
		return DataResult.success(iPage);
	}
	
}
